package ywh.ui.courseslider
{
	import fl.controls.Slider;
	
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author WilliWHYang
	 */
	public class CourseSlider extends Slider
	{
		private var thumbLoader:CourseSliderThumbLoader;
		private var _thumbInfoProvider:IGetInfoByNumber;
		private var _thumbHeight:Number;
		private var _thumbWidth:Number;
		
		public function CourseSlider()
		{
			super();
		}
		
		public function set thumbInfoProvider(value:IGetInfoByNumber):void
		{
			_thumbInfoProvider = value;
		}
		
		[Inspectable(defaultValue=100)]
		public function get thumbHeight():Number
		{
			return _thumbHeight;
		}
		
		public function set thumbHeight(value:Number):void
		{
			_thumbHeight = value;
			thumbLoader.height = value;
			thumbLoader.y = -value - 10;
		}
		
		[Inspectable(defaultValue=100)]
		public function get thumbWidth():Number
		{
			return _thumbWidth;
		}
		
		public function set thumbWidth(value:Number):void
		{
			_thumbWidth = value;
			thumbLoader.width = value;
			thumbLoader.x = track.mouseX - value / 2;
		}
		
		override protected function configUI():void
		{
			super.configUI();
			thumbLoader = new CourseSliderThumbLoader();
			thumbLoader.visible = false;
			this.addChild(thumbLoader);
			/**
			 * 修正滑轨的行为;
			 */
			track.removeEventListener(MouseEvent.CLICK, onTrackClick);
			track.addEventListener(MouseEvent.MOUSE_DOWN, onTrackClick);
		}
		
		override protected function calculateValue(pos:Number, interactionType:String, clickTarget:String, keyCode:int = undefined):void
		{
			super.calculateValue(pos, interactionType, clickTarget, keyCode);
			changeThumb();
		}
		
		protected function changeThumb():void
		{
			if (_thumbInfoProvider)
			{
				var url:String = String(_thumbInfoProvider.getInfoByNumber(value, "")["url"]);
				thumbLoader.x = track.mouseX - thumbLoader.width / 2;
				if (url != thumbLoader.source)
				{
					thumbLoader.source = url;
				}
			}
		}
		
		override protected function thumbPressHandler(event:MouseEvent):void
		{
			thumbLoader.visible = true;
			super.thumbPressHandler(event);
		}
		
		override protected function thumbReleaseHandler(event:MouseEvent):void
		{
			thumbLoader.visible = false;
			super.thumbReleaseHandler(event);
		}
		
		override protected function onTrackClick(event:MouseEvent):void
		{
			thumbPressHandler(event);
			super.onTrackClick(event);
		}
	
	}

}